package com.kun.framework.core.compoment;

import com.kun.framework.core.AbstractApplication;
import com.kun.framework.core.enums.SvgInfoEnum;
import com.kun.framework.core.util.ComUtil;
import com.kun.framework.core.util.SvgStore;
import com.kun.framework.core.util.ThreadPoolUtil;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Popup;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.ref.WeakReference;

/**
 * 弹出提示语，延时自动关闭提示框 ,2.0版本
 *
 * @author gzc
 * @version 2.0
 * @since 2024/8/19
 */
@SuppressWarnings("UNUSED")
public class PopupMessage extends VerifyPopupMessage {
    private final static Logger log = LoggerFactory.getLogger(PopupMessage.class);

    /**
     * 默认时间（毫秒）
     */
    private static final long DEFAULT_TIME = 3000;
    /**
     * 图标默认大小
     */
    private static final double ICON_SIZE = 18;


    /**
     * 成功提示
     *
     * @param message 提示内容
     */
    public static void success(String message) {
        ComUtil.jfxThreadRun(() -> PopupMessage.success(DEFAULT_TIME, message));
    }

    /**
     * 成功提示
     *
     * @param time    延时关闭提示框时间（毫秒）
     * @param message 提示内容
     */
    public static void success(long time, String message) {
        success(time, message, null);
    }

    public static void success(long time, String message, Stage stage) {
        PopupConfig popupConfig = PopupConfig.builder()
                .time(time).message(message)
                .backgroundColor("#f0f9eb")
                .fontColor("#67c23a")
                .build();
        StackPane stackPane = SvgStore.getSvgSizeAndColor(SvgInfoEnum.CHECK, "#67c23a", ICON_SIZE, ICON_SIZE);
        PopupMessage.createPopup(popupConfig, stackPane, stage);
    }

    /**
     * 信息提示
     *
     * @param message 提示内容
     */
    public static void info(String message) {
        ComUtil.jfxThreadRun(() -> PopupMessage.info(DEFAULT_TIME, message));
    }

    /**
     * 信息提示
     *
     * @param time    延时关闭提示框时间（毫秒）
     * @param message 提示内容
     */
    public static void info(long time, String message) {
        info(time, message, null);
    }

    public static void info(long time, String message, Stage stage) {
        PopupConfig popupConfig = PopupConfig.builder()
                .time(time).message(message)
                .backgroundColor("#f4f4f5")
                .fontColor("#909399")
                .build();
        StackPane stackPane = SvgStore.getSvgSizeAndColor(SvgInfoEnum.INFO, "#909399", ICON_SIZE, ICON_SIZE);
        PopupMessage.createPopup(popupConfig, stackPane, stage);
    }

    /**
     * 警告提示
     *
     * @param message 提示内容
     */
    public static void warning(String message) {
        ComUtil.jfxThreadRun(() -> PopupMessage.warning(DEFAULT_TIME, message));
    }

    /**
     * 警告提示
     *
     * @param time    延时关闭提示框时间（毫秒）
     * @param message 提示内容
     */
    public static void warning(long time, String message) {
        warning(time, message, null);
    }

    public static void warning(long time, String message, Stage stage) {
        PopupConfig popupConfig = PopupConfig.builder()
                .time(time).message(message)
                .backgroundColor("#fdf6ec")
                .fontColor("#e6a23c")
                .build();
        StackPane stackPane = SvgStore.getSvgSizeAndColor(SvgInfoEnum.WARNING, "#e6a23c", ICON_SIZE, ICON_SIZE);
        PopupMessage.createPopup(popupConfig, stackPane, stage);
    }

    /**
     * 错误提示
     *
     * @param message 提示内容
     */
    public static void error(String message) {
        ComUtil.jfxThreadRun(() -> PopupMessage.error(DEFAULT_TIME, message));
    }

    /**
     * 错误提示
     *
     * @param time    延时关闭提示框时间（毫秒）
     * @param message 提示内容
     */
    public static void error(long time, String message) {
        error(time, message, null);
    }

    public static void error(long time, String message, Stage stage) {
        PopupConfig popupConfig = PopupConfig.builder()
                .time(time).message(message)
                .backgroundColor("#fef0f0")
                .fontColor("#f56c6c")
                .build();
        StackPane stackPane = SvgStore.getSvgSizeAndColor(SvgInfoEnum.CLOSE, "#f56c6c", ICON_SIZE, ICON_SIZE);
        PopupMessage.createPopup(popupConfig, stackPane, stage);
    }

    /**
     * 创建提示框
     *
     * @param popupConfig 提示框配置
     * @param svg         提示框中icon图标
     */
    private static void createPopup(final PopupConfig popupConfig, StackPane svg, Stage stage) {
        BorderPane borderPane = new BorderPane();
        // 创建提示语
        Label label = new Label(popupConfig.getMessage());
        label.setTextFill(Paint.valueOf(popupConfig.getFontColor()));
        label.setFont(new Font(13));
        label.setPadding(new Insets(0, 0, 0, 5));
        borderPane.setCenter(label);
        borderPane.setPadding(new Insets(9, 11, 9, 11));
        BackgroundFill backgroundFill = new BackgroundFill(
                Paint.valueOf(popupConfig.getBackgroundColor()),
                new CornerRadii(8),
                new Insets(0));
        borderPane.setBackground(new Background(backgroundFill));
        // 创建图标
        if (svg != null) {
            borderPane.setLeft(svg);
        }
        Popup popup = new Popup();
        popup.getContent().add(borderPane);
        WeakReference<Popup> weakReference = new WeakReference<>(popup);
        ComUtil.jfxThreadRun(() -> {
            if (stage != null) {
                popup.show(stage);
            } else {
                popup.show(AbstractApplication.getAppStage());
            }
        });
        // 延时关闭提示框
        ThreadPoolUtil.fastPool().execute(() -> {
            try {
                Thread.sleep(popupConfig.getTime());
                ComUtil.jfxThreadRun(() -> {
                    if (popup.isShowing()) {
                        popup.hide();
                        weakReference.clear();
                    }
                });
            } catch (Exception e) {
                log.error("延时关闭提示语发生异常->", e);
            }
        });
    }


    /**
     * 提示框配置类
     */
    private static class PopupConfig {
        /**
         * 延时关闭时间(毫秒)
         */
        private long time;
        /**
         * 提示信息
         */
        private String message;
        /**
         * 提示框背景颜色
         */
        private String backgroundColor;
        /**
         * 提示框文字颜色
         */
        private String fontColor;

        private PopupConfig() {
        }

        /**
         * 构建者
         */
        public static PopupConfig builder() {
            PopupConfig popupConfig = new PopupConfig();
            popupConfig.time(DEFAULT_TIME);
            popupConfig.message("");
            popupConfig.fontColor("#FAF7F7");
            popupConfig.backgroundColor("#565353");
            return popupConfig;
        }

        public PopupConfig time(long time) {
            this.time = time;
            return this;
        }

        public PopupConfig message(String message) {
            this.message = message;
            return this;
        }

        public PopupConfig backgroundColor(String backgroundColor) {
            this.backgroundColor = backgroundColor;
            return this;
        }

        public PopupConfig fontColor(String fontColor) {
            this.fontColor = fontColor;
            return this;
        }

        public PopupConfig build() {
            return this;
        }

        public long getTime() {
            return time;
        }

        public String getMessage() {
            return message;
        }

        public String getBackgroundColor() {
            return backgroundColor;
        }

        public String getFontColor() {
            return fontColor;
        }
    }
}
